package org.tacademy.mylooper;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class CalcThread extends Thread {

	Handler mainHandler;
	Handler backHandler;
	
	public CalcThread(Handler handler){
		mainHandler = handler;
	}
	
	public void run(){
		Looper.prepare();
		backHandler = new Handler(){
			public void handleMessage(Message msg){
				Message retMessage = new Message();
				
				if(msg.what == 0){		//square
					
					try {
						sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					retMessage.what = 0;
					retMessage.arg1 = msg.arg1 * msg.arg1;
					
				}else if(msg.what == 1){	//root
					
					try {
						sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					retMessage.what = 1;
					retMessage.obj = new Double(Math.sqrt((double)(msg.arg1)));
				}
				mainHandler.sendMessage(retMessage);
			}
		};
		Looper.loop();
	}
}
